#include "utils/path_util.hpp"

namespace boost {
namespace filesystem {

/**
* this function gives the relative path from one path to another one
*
* @param pathRef  the reference path
* @param path     the path we want relative to refPath
*
* @return the relative path
*/
path relative_to(const path& pathRef, const path& path2)
{
	// Trivial case
	if(equivalent(pathRef, path2))
        return path2;

	// Doesn't share a root
	if(!equivalent(pathRef.root_path(), path2.root_path()))
		return path2;

	// We don't care about the root anymore (and makes the rest easier)
	path current  = pathRef.branch_path().relative_path();
	path p        = path2.branch_path().relative_path();

	path final(".", native);

	path::iterator pit = p.begin(), cit = current.begin();
	// Find the shared directory
	for(;pit != p.end() && cit != current.end(); ++pit, ++cit)
		if(*pit != *cit) // May not be right
			break;

	// Put needed parent dirs in
	while(cit != current.end())
	{
		final = ".." / final;
		++cit;
	}

	// Add the path from shared
	while(pit != p.end())
		final /= path(*pit++, native); // Gah! Why doesn't *path::iterator return paths?

    // Add the file
    final /= path2.leaf();

	// .normalize() ?
	return final;
}

};
};
